// worker.js
self.onmessage = function (e) {
    const {data, chunkSize, delay} = e.data;
    let offset = 0;
    let count = 0;

    const sendChunk = () => {
        if (offset >= data.byteLength) {
            self.postMessage({done: true});
            return;
        }

        const end = Math.min(offset + chunkSize, data.byteLength);
        const chunk = data.slice(offset, end);
        self.postMessage({chunk, count: ++count}, [chunk]);

        offset = end;
        if (offset < data.byteLength) {
            setTimeout(sendChunk, delay);
        } else {
            self.postMessage({done: true});
        }
    };

    sendChunk();
};